<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <search ref='search' :config='searchConfig' @getHeight='getHeight' @search='search' />

    <!-- table区域-begin -->
    <div class='page_table'>
      <vxe-grid
        ref='xGrid'
        v-bind='vexConfig'
        :align='left'
        :border='true'
        :columns='columns'
        :data='dataSource'
        :height='pageHeight-seachHeight-90'
        :loading='loading'
        :resizable='true'
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        :showOverflow='true'
        :toolbar-config='tableToolbar'
        size='mini'
        @radio-change='radioChangeEvent'
        :radio-config="{ trigger: 'row'}"
      >
        <template #toolbar_buttons>
          <div class='table-operator'>
            <a-button v-has="'SfaOrderCenter:approval'" :disabled="selectionRows.length!=1||[3,4,5].includes(selectionRows[0].approvalStatus)" @click="handleEdit(selectionRows[0])">审核</a-button>
            <a-button :disabled="selectionRows.length!=1||selectionRows[0].orderStatus!=3" style="margin-bottom: 10px" type="primary" @click="audit(selectionRows[0])">确认发货</a-button>
            <a-button type="primary" @click="handleExportXls('物流单号')">物流单号模板下载</a-button>
            <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader1"
                      :action="importExcelUrl" @change="handleImportExcel">
              <a-button type="primary">物流单号导入</a-button>
            </a-upload>
            <a-button type="primary" @click="downExportXls('sfaOrder_service')">导出</a-button>
            <a-button type="primary" @click="handlePrint(selectionRows[0])">打印</a-button>
          </div>
        </template>
        <template #details="{ row,rowIndex }">
          <a @click="handleDetails(row)">{{ row.sfaOrderCode }}</a>
        </template>
        <!-- <template #detail="{ row,rowIndex }">
          <a @click="handleDetail(row)">{{ row.clientCode }}</a>
        </template> -->
        <template #status='{ row }'>
          <a-switch :checked='row.status' size='small' @change='handleStatus(row,$event)' />
        </template>
        <template #action='{ row:record }'>
          <a @click='handleEdit1(record)'>编辑</a>
<!--          <span v-if='record.fileUrl'>-->
<!--            <a-divider type='vertical' />-->
<!--            <a :href='record.fileUrl'>删除</a>-->
<!--          </span>-->
        </template>
        <template #pager>
          <vxe-pager
            :currentPage.sync='ipagination.current'
            :layouts="['Total', 'PrevJump', 'PrevPage', 'Number', 'NextPage', 'NextJump','Sizes', 'FullJump']"
            :page-size.sync='ipagination.pageSize'
            :pageSizes='ipagination.pageSizeOptions'
            :total='ipagination.total'
            @page-change='handlePageChange'>
          </vxe-pager>
        </template>
      </vxe-grid>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <sfa-order-center-modal ref='modalForm' @ok="modalFormOk"></sfa-order-center-modal>
    <sfa-order-center-print-modal ref='modalPrint'></sfa-order-center-print-modal>
    <deliver-goods ref='modalForm2' @ok="modalFormOk"></deliver-goods>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { PageListMixin } from '@/mixins/PageListMixin'
import { httpAction,postAction,getAction } from '@/api/manage'
import sfaOrderCenterModal from './modules/sfaOrderCenterModal'
import sfaOrderCenterPrintModal from '@views/sfa/order/modules/sfaOrderCenterPrintModal'
import Vue from 'vue'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import deliverGoods from './modules/deliverGoods'
export default {
  name: 'SfaVisitLineList',
  mixins: [PageListMixin],
  components: {
    sfaOrderCenterModal,
    sfaOrderCenterPrintModal,
    deliverGoods
  },
  data() {
    return {
      tokenHeader1: {'Authorization':  'Bearer ' + Vue.ls.get(ACCESS_TOKEN).access_token},
      description: '订单中心',
      //查询区域
      searchConfig:[
        {
          type: 'input',
          name: 'sfaOrderCode',
          placeholder: '订单编号',
        },
        {
          type: 'j-select',
          dictCode: 'order_type',
          name: 'orderType',
          placeholder: '订单类型'
        },
        {
          type: 'input',
          name: 'customerCode',
          placeholder: '门店编码'
        },
        {
          type: 'input',
          name: 'customerName',
          placeholder: '门店名称'
        },
        {
          type: 'j-select',
          dictCode: 'bpm_status',
          name: 'approvalStatus',
          placeholder: '审批状态'
        },
        {
          type: 'j-select',
          dictCode: 'order_status',
          name: 'orderStatus',
          placeholder: '订单状态'
        },
        {
          type: 'input',
          name: 'createName',
          placeholder: '创建人'
        },
        {
          type: 'date-range', //范围选择
          range: ['startDate', 'endDate'],
          name: 'range',
          format: 'YYYY-MM-DD',
          placeholder: ['开始时间', '结束时间']
        }
      ],
      // 表头
      columns: [
        { type: 'radio', width: 50, fixed: 'left' },
        {
          title: '订单编号',
          minWidth:80,
          field: 'sfaOrderCode',
          fixed: 'left',
          slots: { default: 'details' }
        },
        {
          title: '订单类型',
          minWidth:80,
          field: 'orderTypeStr',
        },
        {
          title: '物流单号',
          minWidth:80,
          field: 'logisticsNo',
          // slots: { default: 'detail' }
        },
        {
          title: '门店编码',
          minWidth:80,
          field: 'customerCode'
        },
        {
          title: '门店名称',
          minWidth:80,
          field: 'customerName'
        },
        {
          title: '所属组织',
          minWidth:80,
          field: 'orgName'
        },
        {
          title: '订单金额',
          minWidth:80,
          field: 'countMoney'
        },
        {
          title: '创建人',
          minWidth:80,
          field: 'createName'
        },
        {
          title: '创建时间',
          minWidth:80,
          field: 'createTime'
        },
        {
          title: '更新人',
          minWidth:80,
          field: 'updateName'
        },
        {
          title: '更新时间',
          minWidth:80,
          field: 'updateTime',
        },
        {
          title: '订单状态',
          minWidth:80,
          field: 'orderStatusStr',
        },
        {
          title: '审批流程',
          align: 'left',
          field: 'currentNodeDesc',
          fixed: 'right',
          minWidth: 100
        },
        {
          title: '审批状态',
          align: 'left',
          field: 'approvalStatusStr',
          width: 80,
          fixed: 'right',
          minWidth: 80
        },
        {
          title: '操作',
          field: 'action',
          fixed: 'right',
          width:120,
          slots: {
            default: 'action'
          }
        }
      ],
      isorter: {
        permflag: 'orderapproval_page_impl'
      },
      url: {
        list: window._CONFIG['domianSfaURL'] + '/sfaOrderController/findPageList',
        delete: window._CONFIG['domianSfaURL'] + '/visit/sfaVisitLineController/delete',
        detail: window._CONFIG['domianSfaURL'] + '/sfaOrderController/getInfo',
        deleteBatch: window._CONFIG['domianSfaURL'] + '/visit/sfaVisitLineController/deleteBatch',
        exportXlsUrl: window._CONFIG['domianSfaURL'] + '/sfaOrderController/exportXls',
        importExcelUrl: window._CONFIG['domianSfaURL'] + '/sfaOrderController/importExcel',
        print: window._CONFIG['domianSfaURL'] + '/sfaOrderController/getOrderPrintingById',

      }
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${this.url.importExcelUrl}`
    }
  },

  methods: {
    //发货
    audit(record) {
      console.log(record)
      this.$refs.modalForm2.title = '发货'
      this.$refs.modalForm2.edit(record)
    },
    // {
    //   httpAction(
    //     this.url.fahuo,
    //     {
    //       id: [this.id],
    //       status: '60'
    //     },
    //     'post'
    //   ).then(res => {
    //     this.onClose()
    //     this.$message.success('发货成功')
    //   })
    // },
  //  审批
    handleEdit: async function(record) {
      if (this.url.detail) {
        const res = await getAction(this.url.detail, { id: record.id })
        if (res.success) {
          if (res.result) {
            record = res.result
            console.log(record)
          }
        } else {
          this.$message.error(res.message)
        }
      }
      this.$refs.modalForm.title = '审批'
      this.$refs.modalForm.disableSubmit = true
      this.selectionRows = []
      this.$refs.modalForm.edit(record)
    },
    async handlePrint(record){
      if (this.url.print) {
        const res = await getAction(this.url.print, { id: record.id })
        if (res.success) {
          if (res.result) {
            record = res.result
            console.log(record)
          }
        } else {
          this.$message.error(res.message)
        }
      }
      this.$refs.modalPrint.title = '打印'
      this.$refs.modalPrint.disableSubmit = true
      this.$refs.modalPrint.edit(record)
    },

    //  编辑
    handleEdit1: async function(record) {
      console.log(record)
      if (this.url.detail) {
        const res = await getAction(this.url.detail, { id: record.id })
        if (res.success) {
          if (res.result) {
            record = res.result
            console.log(record)
          }
        } else {
          this.$message.error(res.message)
        }
      }
      this.$refs.modalForm.title = '编辑'
      this.$refs.modalForm.disableSubmit = true
      this.$refs.modalForm.edit(record)
    },
    handleDetails: async function (record) {
      if (this.url.detail) {
        const res = await getAction(this.url.detail, { id: record.id, sfaOrderCode: record.sfaOrderCode })
        if (res.success) {
          if (res.result) {
            record = res.result
            console.log(record)
          }
        } else {
          this.$message.error(res.message)
        }
      }
      this.$refs.modalForm.title = '详情'
      this.$refs.modalForm.disableSubmit = true
      this.$refs.modalForm.edit(record)
    },
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>